package com.qtyx.nms.ui.base

import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.SkinAppCompatDelegateImpl
import androidx.core.view.isVisible
import com.gyf.immersionbar.BarHide
import com.ldd.base.ui.activity.BaseLddActivity
import com.qtyx.nms.R
import com.qtyx.nms.util.DensityUtil
import kotlinx.android.synthetic.main.ac_base.*


/**
 * 基本的Activity
 * 封装了
 */
abstract class BaseActivity :BaseLddActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化设置
        initSetting()
        //设置主布局
        setContentView(R.layout.ac_base)

        //加载子类布局
        if (getLayoutId() != 0) {
            View.inflate(mContext, getLayoutId(), llBaseContent)
        }
        //初始化数据
        initData()

        //初始化返回按钮
        initBack()
    }

    override fun getDelegate(): AppCompatDelegate {
        return SkinAppCompatDelegateImpl.get(this, this)
    }

    /**
     * 初始化设置
     * */
    protected open fun initSetting() {
        initPresenter()
        //适配机型
        DensityUtil.setDensity(application,this)
        //隐藏状态栏 和 导航栏
        mImmersionBar.reset().fullScreen(true).hideBar(BarHide.FLAG_HIDE_BAR).init()
    }

    open fun initPresenter() {}



    //========================================子类实现的方法=============================================================================
    /**设置布局文件的ID */
    protected abstract fun getLayoutId(): Int
    /**初始化数据 */
    protected abstract fun initData()




    //========================================公共返回图标=============================================================================
    /**是否显示返回按钮**/
    var isShowBack=true
    private fun initBack(){
        if(isShowBack){
            ivBaseBackButton.visibility=View.VISIBLE
            //延迟消失
            window.decorView.postDelayed({
                ivBaseBackButton.startAnimation(animHide)
            },3*1000)

        }else{
            ivBaseBackButton.visibility=View.GONE
        }
    }

    /**
     * 触摸屏幕事件
     */
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if(isShowBack){
            when (ev.action) {
                //点击屏幕，显示返回按钮，隔两秒消失
                MotionEvent.ACTION_DOWN -> showBackButton()
            }
        }
        return super.dispatchTouchEvent(ev)
    }


    /**
     * 显示动画
     */
    private val animShow:Animation by lazy {
        val animation=AnimationUtils.loadAnimation(mContext, R.anim.alpha_show)
        animation.setAnimationListener(object :Animation.AnimationListener{
            override fun onAnimationRepeat(animation: Animation?) {}

            override fun onAnimationEnd(animation: Animation?) {
                ivBaseBackButton.visibility=View.VISIBLE
                ivBaseBackButton.isEnabled=true
            }
            override fun onAnimationStart(animation: Animation?) {}
        })
        animation
    }

    /**
     * 消失动画
     */
    private val animHide:Animation by lazy {
        val animation=AnimationUtils.loadAnimation(mContext, R.anim.alpha_hide)
        animation.setAnimationListener(object :Animation.AnimationListener{
            override fun onAnimationRepeat(animation: Animation?) {}

            override fun onAnimationEnd(animation: Animation?) {
                ivBaseBackButton.visibility=View.GONE
                ivBaseBackButton.isEnabled=false
            }
            override fun onAnimationStart(animation: Animation?) {}
        })
        animation
    }


    /**
     * 显示返回按钮
     */
    private fun showBackButton(){
        if(ivBaseBackButton.visibility==View.GONE) {
            ivBaseBackButton.startAnimation(animShow)
            //延迟消失
            window.decorView.postDelayed({
                ivBaseBackButton.startAnimation(animHide)
            }, 3 * 1000)
        }
    }



    /**第一次点击返回按钮的时间**/
    private var firstTime: Long = 0
    /**
     * 标题返回键 点击事件
     */
    fun onFinishActivity(view: View) {
        Log.i(TAG, "=====onFinishActivity=====")
        //=====================防止快速点击左上角返回，关闭上个页面=======================
        val secondTime = System.currentTimeMillis()
        if (secondTime - firstTime < 800) {
            return
        }
        firstTime = secondTime
        //=====================防止快速点击左上角返回，关闭上个页面=======================
        finishActivityCustom()
    }



}